#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

const int MAXN = 1e5 + 10;
const int MOD = 1e9 + 7;

int main() {
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
    ios::sync_with_stdio(false), cin.tie(0);
    int t, n;
    string s;
    cin >> t;
    while (t--) {
        cin >> n >> s;

        set<char> m;
        for (char ch : s)
            m.insert(ch);

        int res = INT_MAX;
        for (char ch : m) {
            int i = 0, j = n - 1;
            int temp = 0;
            while (i < j) {
                if (s[i] == s[j]) {
                    i++, j--;
                    continue;
                }

                if (s[i] != ch && s[j] != ch) {
                    temp = -1;
                    break;
                }

                if (s[i] == ch) {
                    i++, temp++;
                }
                else if (s[j] == ch) {
                    j--, temp++;
                }
            }

            if (temp != -1)
                res = min(res, temp);
        }
        if (res == INT_MAX) res = -1;
        cout << res << endl;
    }


    return 0;
}